package com.cnt.controller;

import com.cnt.dto.LogQueryDTO;
import com.cnt.dto.UserActionDTO;

import com.cnt.dto.UserQueryDTO;
import com.cnt.service.UserInfoService;
import com.cnt.service.UserLogService;
import com.cnt.service.UserService;

import com.cnt.vo.ResultVo;
import com.cnt.vo.UserResultVo.UserInfoVo;
import com.cnt.vo.UserResultVo.queryUserResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/users")
public class UsersController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserLogService userLogService;
    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/queryUser")
    public ResultVo<List<queryUserResultVo>> queryUser(@RequestBody UserQueryDTO userQueryDTO) {
        System.out.println(userQueryDTO);
        List<queryUserResultVo> queryUserList = userService.queryUser(userQueryDTO.getAccount(), userQueryDTO.getPhone(),
                userQueryDTO.getQueryType(), userQueryDTO.getQueryStatus(), userQueryDTO.getQuerySort());
        if (queryUserList == null || queryUserList.isEmpty()) {
            return new ResultVo(1, "查找不到，请更换参数", null);
        }
        return new ResultVo(0, "查询成功", queryUserList);
    }

    // 更改管理员权限状态
    @PostMapping("/changeUserRole")
    public ResultVo changeUserRole(@RequestBody UserActionDTO userActionDTO) {
        System.out.println(userActionDTO);
        return userService.changeUserRole(userActionDTO.getUserId(), userActionDTO.getOperatorId());
    }

    // 删除用户
    @PostMapping("/deleteUser")
    public ResultVo deleteUser(@RequestBody UserActionDTO userActionDTO) {
        return userService.deleteUserById(userActionDTO.getUserId(), userActionDTO.getOperatorId());
    }

    // 用户状态改变
    @PostMapping("/updateUserStatus")
    public ResultVo updateUserStatus(@RequestBody UserActionDTO userActionDTO) {

        return userService.updateUserStatus(userActionDTO.getUserId(), userActionDTO.getOperatorId());
    }

    //返回日志
    @PostMapping("/queryLogs")
    public ResultVo queryLogs(@RequestBody LogQueryDTO logQueryDTO) {
        if ("".equals(logQueryDTO.getStartTime())) {
            logQueryDTO.setStartTime(null); // 或者忽略传递
        }

        if ("".equals(logQueryDTO.getEndTime())) {
            logQueryDTO.setEndTime(null);
        }
        return userLogService.queryLogs(logQueryDTO.getUserId(), logQueryDTO.getAccount(), logQueryDTO.getQueryType(), logQueryDTO.getStartTime(), logQueryDTO.getEndTime());
    }

    @GetMapping("/getAdministrators")
    public ResultVo<List<UserInfoVo>> getAdministrators() {
        return userInfoService.getAdministrators();
    }
}
